Option Strict Off
Option Explicit On
Friend Class frmSourceSetup
	Inherits System.Windows.Forms.Form
	
	'****************Important, if you cant get image from the picturebox, look in earlier example, there is a routine to send image to memory dc.
	
	Dim LoopRunning As Boolean
	Dim CanExitForm As Boolean
	Dim LastCameraListed As Boolean 'This sets high if last camera is in the new source list
	Dim SelectWait As Boolean 'This avoids the select button being activated twice
	
	
	
	
	
	
	Private Sub frmSourceSetup_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		On Error GoTo ErrorHandling 'If an error occurs go to the error handling routine
		
		ErrorCodeNo = "01.00" 'Set an error code for the next section of program
		'The form now loads but remains invisible
		CanExitForm = False 'Disable the user from exiting the  form until a source device has been set
		SelectWait = False 'Enable the select button
		
		LastCameraListed = False
		'UPGRADE_NOTE: The Following line was commented to give the same effect as VB6. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="1C44C272-DA6E-4A2A-BDC5-9A27F0D03D31"'
		'Me.FormBorderStyle = 0
		'UPGRADE_ISSUE: Form property frmSourceSetup.ScaleMode is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="8027179A-CB3B-45C0-9863-FAA1AF983B59"'
        'frmSourceSetup.ScaleMode = 3 'Set scalemode to pixels
		Me.Width = VB6.TwipsToPixelsX(710 * VB6.TwipsPerPixelX)
		Me.Height = VB6.TwipsToPixelsY(225 * VB6.TwipsPerPixelY)
		Me.Left = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) / 2) - ((710 / 2) * VB6.TwipsPerPixelX)) 'Set the window to be in the middle of the screen
		Me.Top = VB6.TwipsToPixelsY((VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) / 2) - (((225 / 2) + 28) * VB6.TwipsPerPixelY)) 'Set the window raised up slightly. So it is better positioned when running at 1024 x 768
		
		'UPGRADE_ISSUE: PictureBox property picFacia1.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'picFacia1.ScaleMode = 3
		'UPGRADE_ISSUE: PictureBox property picFacia1.AutoSize was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
		picFacia1.AutoSize = True : picFacia1.Left = 0 : picFacia1.Top = 0
		'UPGRADE_ISSUE: PictureBox property picFacia1.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'picFacia1.AutoRedraw = True : picFacia1.BorderStyle = System.Windows.Forms.BorderStyle.None
		picFacia1.Visible = True
		picFacia1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
		'UPGRADE_WARNING: Picture has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		picFacia1.BackgroundImage = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Source_S.jpg") 'Setup the correct picture size etc.
		
		'UPGRADE_WARNING: Control property picFacia1.Parent was upgraded to picFacia1.FindForm which has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="DFCDE711-9694-47D7-9C50-45A99CD8E91E"'
		SetWindowRgn(picFacia1.FindForm.Handle.ToInt32, RegionSource, True) 'Set the region of the Source box outside which nothing will be displayed. The region has been
		
		
		
		'********************Load all the possible devices into the list window.
		
		ErrorCodeNo = "01.01" 'Set an error code for the next section of program
		Dim FirstCheck As Boolean 'Flag so if no devices are found it doesn't keep trying to delete CaptureFormatData.mt file
		FirstCheck = True
		
checkagain: 
		System.Windows.Forms.Application.DoEvents()
		Call PopulateSourceList() 'Fill the source list box with all the possible video source devices
		
		'Now check if the last used device is in the list.
		Dim XL As Integer
		For XL = 0 To lstSource.Items.Count - 1
			If VB6.GetItemString(lstSource, XL) = LastCamera Then LastCameraListed = True
		Next XL
		
		ErrorCodeNo = "01.02" 'Set an error code for the next section of program
		
		ErrorCodeNo = "01.03" 'Set an error code for the next section of program
		Me.Show() 'The form has been set up so we can now show it to the user
		
		If lstSource.Items.Count < 1 Then 'There are no source devices so reset all files and loop
			lblInstruction.Text = "Please install a DirectX (Directshow) compatible source device and make sure it is switched on" 'Ask the user to select a device
			If FirstCheck = True Then
				'There are no devices in the list, this means the captureformatdata file is invalid. If this file existed and a camera that was connected was reconnected, the program
				'would immediately try to stream from it. This causes an error as it tries to stream before the camera is initiated.
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(My.Application.Info.DirectoryPath & "\CaptureFormatData.mt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\CaptureFormatData.mt")) 'Erase the capture format file
				LastCamera = "NOCAMERA"
				FirstCheck = False
				LastCameraListed = False
			End If
			GoTo checkagain
		End If
		
		If LastCameraListed = False Then 'If there is no previous camera then set it to the first in the list
			lstSource.SelectedIndex = 0 'Select first device in list by default
			lblInstruction.Text = "Select Source then press Arrow Select Button" 'Ask the user to select a device
		Else
			lblInstruction.Text = "Please wait whilst your previous camera settings are loaded" 'There is a previous device in the list so tell the user program is loading
			Call SelectSourceButton() 'The previously used camera is listed so trigger streaming automatically
		End If
		
		
		ErrorCodeNo = "01.99" 'Set an error code for the next section of program
		
		On Error GoTo 0 'Set error handling so it doesnt return here.
		Exit Sub
		
		'***********************************************************************************************************************************************
ErrorHandling: 
		ErrorDescription = "Source Setup Problem"
		Call frmErrorBox.ErrorMessageBox() 'Put up a message Box
		Call ExitProgram() 'Exit the Program gracefully
	End Sub
	Private Sub PopulateSourceList() 'Fills the source list with all the available video devices
		Dim catlist As FSFWRAPLib.IVBCollection
		Dim fce As FSFWRAPLib.FilterCatEnumerator
		Dim f As FSFWRAPLib.IFilterClass
		fce = New FSFWRAPLib.FilterCatEnumerator
		catlist = fce.Category("{860BB310-5D01-11d0-BD3B-00A0C911CE86}") 'Video Capture devices only  further GUIDs can be found in DirectShow sdk uuids.h (Google it!)
		
		lstSource.Items.Clear() 'Clear the listbox of any source device names
		
		For	Each f In catlist
			lstSource.Items.Add(f.Name)
		Next f
	End Sub
	
	Private Sub SelectSourceButton() 'This is the button to select the video
		LoopRunning = False 'stop any previous loops running'
		
		If lstSource.Items.Count < 1 Then Exit Sub 'No source devices, so you cannot select one
		If lstSource.SelectedItems.Count = 0 Or lstSource.SelectedItems.Count > 1 Then 'Check if one source is selected from the list
			If LastCameraListed = False Then 'If not, check that the last source is in the file
				Call MsgBox("Select one source device from the left hand box", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
				Exit Sub
			End If
		End If
		lstDetails.Items.Clear() 'Clear the listbox of any resolution details etc.
		
		If SelectWait = True Then Exit Sub 'The select button has been pressed again before last call is complete so exit sub
		SelectWait = True 'Disable the select button from calling up a second properties box
		'UPGRADE_ISSUE: PictureBox method picBufferMain.Cls was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picBufferMain.Cls() 'Clear the alphablend window. Must be done otherwise lower resolutions selected after larger
        'resolutions dont fill the preview window

        frmAveraging.picBufferMain.CreateGraphics.Clear(System.Drawing.Color.Transparent)


		ErrorCodeNo = "01.04a" 'Set an error code for the next section of program
		
		If LastCameraListed = True Then
			ErrorCodeNo = "01.04b" 'Set an error code for the next section of program
			LastCameraListed = False 'Only trigger auto streaming once using saved values
			'leave the lastcamera value unaltered
			lstDetails.Items.Add(LastCamera) 'Enter the camera name in the right hand display box
			lstDetails.Refresh()
			Call SetVideoStream((frmAveraging.picCamera.Handle.ToInt32), LastCamera, True) 'Setup DirectShow Streaming from camera to picturebox (hardware overlay on picturebox)
		Else
			ErrorCodeNo = "01.04c" 'Set an error code for the next section of program
			LastCamera = lstSource.Text 'Set the camera source to thats elected by the user
			lstDetails.Items.Add(LastCamera) 'Enter the camera name in the right hand display box
			lstDetails.Refresh()
			lblInstruction.Text = "Select Output Size in Pin Properties box then hit OK - There may be a delay whilst camera loads."
			Call SetVideoStream((frmAveraging.picCamera.Handle.ToInt32), LastCamera, False) 'Setup DirectShow Streaming from camera to picturebox (hardware overlay on picturebox)
			
		End If
		
		ErrorCodeNo = "01.04d" 'Set an error code for the next section of program
		'Define the size of the alphastore already defined on the frmMainForm
		frmAveraging.picBufferMain.Width = CaptureWidth 'Set the picturebox to the height of the video resolution
		frmAveraging.picBufferMain.Height = CaptureHeight 'Set the picturebox to the width of the video resolution
		
		
		lstDetails.Items.Add("Video Resolution = " & Str(CaptureWidth) & "  X " & Str(CaptureHeight)) 'Get the resolution of the video source and show it in list box
		
		lblInstruction.Text = "If you are happy with the source and resolution press <Continue> or use the arrow button to reselect"
		
		CanExitForm = True 'A source has been set, so the user can exit the form
		SelectWait = False 'Enable the select button should the user require a further change
		
		
		'Set up the picturebox storage areas to the correct sizes
		
		frmAveraging.picRaw.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picRaw.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picRaw.AutoRedraw = True
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picRaw.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picRaw.ScaleMode = vbPixels
		frmAveraging.picRaw.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picRaw.Top = 0
		frmAveraging.picRaw.Left = 0
		frmAveraging.picRaw.Width = CaptureWidth
		frmAveraging.picRaw.Height = CaptureHeight
		
		frmAveraging.picAverage.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picAverage.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picAverage.AutoRedraw = True
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picAverage.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picAverage.ScaleMode = vbPixels
		frmAveraging.picAverage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picAverage.Top = 0
		frmAveraging.picAverage.Left = 0
		frmAveraging.picAverage.Width = CaptureWidth
		frmAveraging.picAverage.Height = CaptureHeight
		
		frmAveraging.picAverage.ForeColor = System.Drawing.Color.Lime 'Set up the font for printing framenumbers on captured frames
		frmAveraging.picAverage.Font = VB6.FontChangeBold(frmAveraging.picAverage.Font, True)
		frmAveraging.picAverage.Font = VB6.FontChangeSize(frmAveraging.picAverage.Font, CaptureWidth / 60)
		'UPGRADE_WARNING: Only TrueType and OpenType fonts are supported in Windows Forms. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="971F4DF4-254E-44F4-861D-3AA0031FE361"'
		frmAveraging.picAverage.Font = VB6.FontChangeName(frmAveraging.picAverage.Font, "MS Sans Serif")
		
		frmAveraging.picCamera.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picCamera.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picCamera.AutoRedraw = True
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picCamera.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picCamera.ScaleMode = vbPixels
		frmAveraging.picCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picCamera.Top = 0
		frmAveraging.picCamera.Left = 0
		frmAveraging.picCamera.Width = CaptureWidth
		frmAveraging.picCamera.Height = CaptureHeight
		
		frmAveraging.picBufferMain.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picBufferMain.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picBufferMain.AutoRedraw = True
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picBufferMain.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picBufferMain.ScaleMode = vbPixels
		frmAveraging.picBufferMain.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picBufferMain.Top = 0
		frmAveraging.picBufferMain.Left = 0
		frmAveraging.picBufferMain.Width = CaptureWidth
		frmAveraging.picBufferMain.Height = CaptureHeight
		
		frmAveraging.picOnion1.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picOnion1.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picOnion1.AutoRedraw = True
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picOnion1.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picOnion1.ScaleMode = vbPixels
		frmAveraging.picOnion1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picOnion1.Top = 0
		frmAveraging.picOnion1.Left = 0
		frmAveraging.picOnion1.Width = CaptureWidth
		frmAveraging.picOnion1.Height = CaptureHeight
		
		frmAveraging.picBackground.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picBackground.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picBackground.AutoRedraw = True
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picBackground.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picBackground.ScaleMode = vbPixels
		frmAveraging.picBackground.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picBackground.Top = 0
		frmAveraging.picBackground.Left = 0
		frmAveraging.picBackground.Width = CaptureWidth
		frmAveraging.picBackground.Height = CaptureHeight
		
		frmAveraging.picComposite.BorderStyle = System.Windows.Forms.BorderStyle.None
		'UPGRADE_ISSUE: PictureBox property picComposite.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picComposite.AutoRedraw = True
        'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picComposite.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picComposite.ScaleMode = vbPixels
		frmAveraging.picComposite.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal
		frmAveraging.picComposite.Top = 0
		frmAveraging.picComposite.Left = 0
		frmAveraging.picComposite.Width = CaptureWidth
		frmAveraging.picComposite.Height = CaptureHeight
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(ChromaBackground) <> "" And Len(ChromaBackground) > 0 Then 'Check if the background image exists
			frmAveraging.picBackground.Image = System.Drawing.Image.FromFile(ChromaBackground) 'Load the picture into the background picturebox ready for chroma keying
		Else
			ChromaType = "Off" 'There has been no chroma key background found, so turn chroma keying off
			ChromaKeyingYesNO = False
		End If
		
		AspRatio = CaptureWidth / CaptureHeight 'Calculate the aspect ratio of the image. (normally 4/3)
		
		'UPGRADE_ISSUE: Load statement is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321"'
        'Load(frmCapture) 'Load the live/playback window . It is still hidden to the user
        'TODO cannot call Load Form in VB.Net - Have to figure out how to handle this
		'set the initial sizes for the capture window. Calculate from the percentage of screen size values loaded in by the LoadSettings module
		'UPGRADE_ISSUE: Form property frmCapture.ScaleMode is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="8027179A-CB3B-45C0-9863-FAA1AF983B59"'
        'frmCapture.ScaleMode = 3
		frmCapture.Width = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) * (FormCaptureWidth / 100)))
		frmCapture.Height = VB6.TwipsToPixelsY(VB6.PixelsToTwipsX(frmCapture.Width) / AspRatio + (TitleHeight * VB6.TwipsPerPixelX)) 'adjusted to accomodate the title bar of form
		
		
		'set the initial size of the capture image and the playback picturebox in the capture window
		frmCapture.picLive.Width = (VB6.PixelsToTwipsX(frmCapture.Width) / VB6.TwipsPerPixelX)
		frmCapture.picLive.Height = frmCapture.picLive.Width / AspRatio
		frmCapture.picLive.Top = 0
		frmCapture.picLive.Left = 0
		frmCapture.picPlayBack.Width = (VB6.PixelsToTwipsX(frmCapture.Width) / VB6.TwipsPerPixelX)
		frmCapture.picPlayBack.Height = frmCapture.picLive.Width / AspRatio
		frmCapture.picPlayBack.Top = 0
		frmCapture.picPlayBack.Left = 0
		AspFix = True 'Now lock the aspect ratio, so that when the user resizes the window aspect ratio is maintained.
		frmCapture.picLive.Visible = True 'Make sure the live window is showing
		frmCapture.picPlayBack.Visible = False 'Make sure the playback window is hidden
		frmCapture.Top = 0
		frmCapture.Left = VB6.TwipsToPixelsX(XsheetWidth * VB6.TwipsPerPixelX) 'Set the form left to make room for the Xsheet whenit is switched on
		
		frmCapture.Show() 'show the live/playback window
		
		'Now begin a loop to setup and show video
		LoopRunning = True
		Do While LoopRunning = True
			System.Windows.Forms.Application.DoEvents() 'Check for events such as finished button being pressed etc.
			
            Dim liveGraph As Graphics = frmCapture.picLive.CreateGraphics
            Dim liveHDC As IntPtr = liveGraph.GetHdc

            Dim avgGraph As Graphics = frmAveraging.picBufferMain.CreateGraphics
            Dim avgHDC As IntPtr = avgGraph.GetHdc


            'UPGRADE_ISSUE: PictureBox property picBufferMain.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            CopyCamToPic(frmAveraging.picBufferMain) 'begin showing the camera images in the display window
			'UPGRADE_ISSUE: Constant STRETCHMODE was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
			'UPGRADE_ISSUE: PictureBox property picLive.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            Call SetStretchBltMode(liveHDC, Declares.StretchMode.STRETCH_HALFTONE)
			'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
			'UPGRADE_ISSUE: PictureBox property picBufferMain.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			'UPGRADE_ISSUE: PictureBox property picLive.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            Call StretchBlt(liveHDC, 0, 0, frmCapture.picLive.Width, frmCapture.picLive.Height, avgHDC, 0, 0, CaptureWidth, CaptureHeight, vbSrcCopy)
			frmCapture.picLive.Refresh()

            liveGraph.ReleaseHdc(liveHDC)
            liveGraph.Dispose()
            avgGraph.ReleaseHdc(avgHDC)
            avgGraph.Dispose()


			Call WindowDragging() 'Drag the button Window
			Call frmCapture.frmCaptureResize() 'Check and adjust window if the user is resizing the live /playback window
			
			BringWindowToTop(Me.Handle.ToInt32) 'Bring the buttons window to the front
		Loop 
		
		
		
		
		
		
		ErrorCodeNo = "01.04" 'Set an error code for the next section of program
	End Sub
	Private Sub QuitButton()
		
		If lstSource.Items.Count < 1 Then 'There are no source devices so just end the program
			CleanDC(Hotspot_Source)
			CleanDC(Hotspot_Buttons)
			CleanDC(Hotspot_Settings)
			frmCapture.Close() 'unload the forms. Must be done as Aspect ratio module when used will crash program
			End
		End If
		
		If MsgBox("Do you want to exit The Helium Frog Animator?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Helium Frog Animator") = MsgBoxResult.Yes Then 'Give the user a chance to return
			CleanDC(Hotspot_Source)
			CleanDC(Hotspot_Buttons)
			CleanDC(Hotspot_Settings)
			frmCapture.Close() 'unload the forms. Must be done as Aspect ratio module when used will crash program
			End
		End If
	End Sub
	Private Sub ContinueButton()
		
		If CanExitForm = False Then
			lblInstruction.Text = "You cannot continue before selecting a source device!"
			Beep()
			Exit Sub 'If the user hasnt selected a source file, the user cannot continue to the main program
		End If
		lblInstruction.Text = "Please wait whilst main program loads"
		lblInstruction.Refresh()
		LoopRunning = False 'Stop the loop on this form
		
		ActiveWindowIs = "ButtonWindow" 'Tell program Button window is the one active
		
		Me.Close() 'Unload the visible part of the form. This hides the form from the user and removes all variables etc.
		'UPGRADE_NOTE: Object frmSourceSetup may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'

        'Me = Nothing 'Unload the invisible part of the form. This erases all variables etc. (might not be required)
        Me.Dispose()
		
		'UPGRADE_ISSUE: Load statement is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321"'
        'Load(frmAnimator) 'Load the buttons form. Program control is now passed to the form load routine of the frmanimator form.
        'TODO - Can;t call load event in VB.Net so need to figure how to do this 
		
	End Sub
	Private Sub picFacia1_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles picFacia1.MouseUp
		Dim Button As Short = eventArgs.Button \ &H100000
		Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
		Dim X As Single = eventArgs.X
		Dim Y As Single = eventArgs.Y 'Mouse button has been released over the source form picturebox (skin)
		If Button = VB6.MouseButtonConstants.LeftButton Then 'The mouse button is the left button
			
			Call GetCursorPos(MousePoint) 'Get the screen position of the cursor
			Call ScreenToClient(picFacia1.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of Buttons picture window
			PixCol = GetPixel(Hotspot_Source, MousePoint.X, MousePoint.Y) 'Get the relative colour at the mouse point of the hotspot image in memory
			Red = PixCol And &HFF : Green = CShort(PixCol And &HFF00) / 256 : Blue = CShort(PixCol And &HFF0000) / 65535 'Get the value of each color byte (rgb)
			If Red = 255 And Green = 0 And Blue = 255 Then Call SelectSourceButton() 'You have released the Source button
			If Red = 255 And Green = 255 And Blue = 0 Then Call QuitButton() 'You have released the quit button
			If Red = 0 And Green = 0 And Blue = 255 Then Call ContinueButton() 'You have released the Continue Button
			
		End If
	End Sub
	Private Sub WindowDragging() 'Handles any dragging of the source setup form
		If GetAsyncKeyState(VK_LBUTTON) Then 'Check the Left mouse is being pressed
			If DragWindow(picFacia1.Handle.ToInt32, Hotspot_Source) = True Then 'If true, the mouse is over the body of the button window and not over a button.
				ReleaseCapture() 'Release the mouse button from the previous command
				SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0) 'Begin dragging the form until mouse button is released
			End If
		End If
	End Sub
End Class